#include<bits/stdc++.h>

using namespace std;
int n;
map<string,int>lby;
int a[5];
char op[105],ad[105];
string AD;
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%s",op+1);
		scanf("%s",ad+1);
		int len=strlen(ad+1);
		bool pd=false;
		int now=1;
		a[1]=a[2]=a[3]=a[4]=a[5]=0;
		AD="";
		int last=0;
		for(int j=1;j<=len;j++){
			AD+=ad[j];
			if(!isdigit(ad[j])&&ad[j]!='.'&&ad[j]!=':'){
				pd=true;
				break;
			}
			if(isdigit(ad[j])){
				if(last<j-1&&!a[now]){
					pd=true;
					break;
				}
				a[now]*=10;
				a[now]+=ad[j]-'0';
				if((now<=4&&a[now]>255)||(now==5&&a[now]>65535)){
					pd=true;
					break;
				}
			}
			if(ad[j]=='.'){
				if(now>3){
					pd=true;
					break;
				}
				if(last==j-1){
					pd=true;
					break;
				}
				last=j;
				now++;
			}
			if(ad[j]==':'){
				if(now!=4){
					pd=true;
					break;
				}
				if(last==j-1){
					pd=true;
					break;
				}
				last=j;
				now++;
			}
		}
		if(last==len||pd||now!=5){
			puts("ERR");
			continue;
		}
		if(op[1]=='S'){
			if(lby[AD])puts("FAIL");
			else lby[AD]=i,puts("OK");
		}
		else {
			if(!lby[AD])puts("FAIL");
			else printf("%d\n",lby[AD]);
		}
	}
	return 0;
}
